<template>
  <div class="search-components">
    <div class="search-component">
      <div class="row keyword">
        <p class="label">关键词</p>
        <el-input
          v-model="searchModel.code"
          style="width: 220px"
          placeholder="设备编号"
          @keyup.enter="handleSearch"
        />
      </div>
      <div class="row keyword">
        <p class="label">设备厂家</p>
        <el-select
          v-model="searchModel.vendor"
          filterable
          style="width: 220px"
          placeholder="设备厂家"
        >
          <el-option
            v-for="item in vendor.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>

      <div class="row keyword">
        <p class="label">使用状态</p>
        <el-select
          v-model="searchModel.state"
          filterable
          style="width: 220px"
          placeholder="使用状态"
        >
          <el-option
            v-for="item in state.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="row">
        <p class="label">在线状态</p>
        <el-select
          v-model="searchModel.status"
          filterable
          style="width: 220px"
          placeholder="在线状态"
        >
          <el-option
            v-for="item in status.data"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </div>
      <div class="search-btn">
        <el-button class="reset-btn" type="primary" @click="handleReset">
          <img class="icon-img" :src="iconReset" alt="" />
          重置</el-button
        >
        <el-button class="submit-btn" type="primary" @click="handleSearch">
          <img class="icon-img" :src="iconSearch" alt="" />
          查询</el-button
        >
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import iconSearch from "@/assets/image/btn/icon-search.png";
import iconReset from "@/assets/image/btn/icon-reset.png";
import {
  getEquipmentSupplier,
  getDictionary,
} from "@/service/modules/publicModule";

const emits = defineEmits(["search"]);

const searchModel = ref({
  code: "",
  vendor: "",
  state: 1,
  status: "",
});

const vendor = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
  ],
});

const state = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
  ],
});

const status = reactive({
  data: [
    {
      id: "",
      name: "全部",
    },
    {
      id: 1,
      name: "在线",
    },
    {
      id: 0,
      name: "离线",
    },
  ],
});

const getData = async () => {
  await getEquipmentSupplier("3").then((res) => {
    if (Array.isArray(res.data.result)) {
      if (res.data.result[0].name !== "全部") {
        res.data.result.unshift(vendor.data[0]);
      }
      vendor.data = res.data.result;
    }
  });
  await getDictionary("device_use_state").then((res) => {
    if (Array.isArray(res.data.result)) {
      if (res.data.result[0].name !== "全部") {
        res.data.result.unshift(state.data[0]);
      }
      state.data = res.data.result;
    }
  });
};

getData();

const handleReset = () => {
  searchModel.value = {};
  emits("search", searchModel.value);
};

const handleSearch = () => {
  // console.log(`output-> searchModel.value`, searchModel.value);
  emits("search", searchModel.value);
};
</script>

<style lang="less" scoped>
.search-components {
  .search-component {
    display: flex;
    flex-wrap: wrap;

    .row {
      display: flex;
      align-items: center;
      margin-bottom: 16px;
    }
    .label {
      font-size: 14px;
      color: #333333;
      margin-right: 8px;
      font-weight: 400;
    }
    .keyword {
      margin-right: 32px;
    }
  }
  .search-btn {
    margin-left: 50px;
    display: flex;
    .reset-btn {
      background: #f2f3f5;
      border: 1px solid #f2f3f5;
      border-radius: 2px;
      color: #333333;
      padding: 5px 16px;
    }
    .submit-btn {
      background: #007aff;
      border: 1px solid #007aff;
      border-radius: 2px;
      padding: 5px 16px;
    }
    .icon-img {
      width: 16px;
      height: 16px;
      margin-right: 8px;
    }
  }
}
</style>